package com.vay.de.dang.online.web;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vay.de.dang.online.VnApp;
import com.vay.de.dang.online.entiy.ModifyAccountEntity;
import com.vay.de.dang.online.resp.RCollectionAccountDto;
import com.vay.de.dang.online.resp.RIdCardDto;
import com.vay.de.dang.online.util.VnContext;
import com.vay.de.dang.online.util.RequestHelper;
import com.vay.de.dang.online.util.ResponseHelper;
import com.vay.de.dang.online.resp.RCustomerDataDto;
import com.vay.de.dang.online.resp.RExchangePlanDto;

public class LoanApi extends BaseApi {

    public static void queryCollectionAccount(ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    //String url = wrapperUrl("loanSupermarket/incomingParts/getCollectionAccount");
                    String url = wrapperUrl("aCGyipT/HeDo/OmxvIp");
                    String json = doPostByJson(url, "{}");
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.toObjectByData(json, RCollectionAccountDto.class));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    public static void modifyCollectionAccount(ModifyAccountEntity entity, ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    String param = JsonUtil.objectToJson(entity);
                    //String url = wrapperUrl("loanSupermarket/incomingParts/updCollectionAccount");
                    String url = wrapperUrl("bzAupJ/FMmRL/RUfz");
                    String json = doPostByJson(url, param);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok("");
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    public static void queryApplyProduct(ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    String no = RequestHelper.currentCommodityNum;
                    //String url = wrapperUrl("loanSupermarket/incomingParts/getLoanFinancialProducts/" + no);
                    String url = wrapperUrl("UpKdn/lYRqONXK/ROWIX/" + no);
                    String json = doGet(url);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.toFinancials(json));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 申请前查询用户资料
    public static void queryCustomerData(ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    // 1. 查询紧联系人
                    //String url = wrapperUrl("loanSupermarket/incomingParts/getEmergencyContactList");
                    String url = wrapperUrl("nmIONYJI/niN/QRUKx");
                    String json = doGet(url);
                    if (JsonUtil.isOk(json)) {
                        ResponseHelper.setEmergencyContact(JsonUtil.toREmergencyContact(json));
                    }

                    // 2. 查询用户基本资料
                    //url = wrapperUrl("loanSupermarket/incomingParts/getCustomerData");
                    url = wrapperUrl("cTR/VykW/RIDjFYms");
                    json = doPostByJson(url, "{}");
                    if (JsonUtil.isOk(json)) {
                        ResponseHelper.setCustomerData(JsonUtil.toObjectByData(json, RCustomerDataDto.class));
                        return ReqResult.ok("");
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 还款计划
    public static void queryExchangePlan(String cNum, String pCode, ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    //String url = wrapperUrl("loanSupermarket/incomingParts/getExchangePlan/" + cNum + "/" + pCode);
                    String url = wrapperUrl("qhsOrgA/vdd/nyXPsvp/" + cNum + "/" + pCode);
                    String json = doGet(url);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.toObjectByData(json, RExchangePlanDto.class));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 提交
    public static void reschedulingSubmission(String cNum, String pCode, ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    Map<String, Object> map = new HashMap<>();
                    map.put("channel", VnApp.getChannel());
                    map.put("commodityNum", cNum);
                    map.put("financialProductsCode", pCode);
                    String param = JsonUtil.objectToJson(map);
                    //String url = wrapperUrl("loanSupermarket/incomingParts/reschedulingSubmission");
                    String url = wrapperUrl("rMm/LQKTTBeB/uFum");
                    String json = doPostByJson(url, param);

                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.readData(json));
                    } else {
                        String error = JsonUtil.readMsg(json);
                        return ReqResult.error(error);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 查询推荐
    public static void queryRecommend(ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    VnContext localUser = VnContext.instance();
                    //String url = wrapperUrl("loanSupermarket/home/getLoanCommodity");
                    String url = wrapperUrl("fQcNx/iOsqGNt/QQs");
                    Map<String, Object> param = new HashMap<>();
                    param.put("isTourist", localUser.isLogin() ? "1" : 0);
                    param.put("pageNo", 1);
                    param.put("pageSize", 10);
                    // 类型: 0 app 显示 1：推荐显示
                    param.put("type", "1");
                    String str = JsonUtil.objectToJson(param);
                    String json = doPostByJson(url, str);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.toLoanCommodityList(json));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 一键申请
    public static void onClickApply(List<String> list, ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    //String url = wrapperUrl("loanSupermarket/incomingParts/oneClickApplication");
                    String url = wrapperUrl("OCr/kLKXHDL/EFnpEb");
                    Map<String, Object> param = new HashMap<>();
                    param.put("channel", VnApp.getChannel());
                    param.put("commodityNums", list);
                    String str = JsonUtil.objectToJson(param);
                    String json = doPostByJson(url, str);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok("");
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 身份识别
    public static void orcIdentify(File file, ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    //String url = wrapperUrl("loanSupermarket/incomingParts/idcard/identify");
                    String url = wrapperUrl("oyoXIMjK/ohrY/PqV/Ygs");
                    String json = doPostFile(url, file);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.toObjectByData(json, RIdCardDto.class));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

}
